package com.prp.gwt.client.widgets.user.friend;

import java.util.HashSet;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.MultiWordSuggestOracle;
import com.google.gwt.user.client.ui.SuggestBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.SuggestOracle.Suggestion;
import com.prp.gwt.client.user.friend.FriendListener;
import com.prp.gwt.client.user.php.PHPHistoryHandler;
import com.prp.gwt.client.vo.UserList;
import com.prp.gwt.client.widgets.MessageDialogBox;

public class RecommendListPanel extends FlowPanel {

	class RecommendItem extends HorizontalPanel {
		public RecommendItem(final int id, String userName, String portrait) {
			addStyleName("friendRecommend-item");
			VerticalPanel vPanel = new VerticalPanel();
			Image image = new Image(portrait);
			vPanel.add(image);
			HTML name = new HTML("<a>" + userName + "</a>");
			vPanel.add(name);
			name.addClickHandler(new ClickHandler(){
				@Override
				public void onClick(ClickEvent event) {
					History.newItem(PHPHistoryHandler.PREFIX_USERHOME+id);
				}
			});
			vPanel.setSpacing(5);
			image.setWidth("50px");
			setSpacing(5);
			add(vPanel);
			HTML remove = new HTML("<a>X</a>");
			add(remove);
			remove.addClickHandler(new ClickHandler() {
				@Override
				public void onClick(ClickEvent event) {
					RecommendItem.this.removeFromParent();
					set.remove(id);
					RecommendPanel.getInstance().set.remove(id);
				}
			});
		}
	}

	private static MultiWordSuggestOracle oracle = new MultiWordSuggestOracle();
	private SuggestBox inputBox = new SuggestBox(oracle);
	private HTML addButton = new HTML("<a>添加</a>");
	private HTML cancelButton = new HTML("<a>取消</a>");
	public HashSet<Integer> set = new HashSet<Integer>();
	private HTML title = new HTML();
	
	public void init() {
		clear();
		add(title);
		add(addButton);
		set.clear();
	}

	public static MultiWordSuggestOracle getSuggestOracle() {
		return oracle;
	}

	public RecommendListPanel(String title) {
		this.title.setHTML(title);
		this.title.addStyleName("friendRecommend-title");
		add(this.title);
		add(addButton);
		inputBox.setWidth("80px");
		addButton.addStyleName("friendRecommend-button");
		inputBox.addStyleName("friendRecommend-input");
		cancelButton.addStyleName("friendRecommend-button");
		addButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				inputBox.setText("");
				int column = getWidgetCount();
				remove(column - 1);
				add(inputBox);
				add(cancelButton);
			}
		});
		cancelButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				int column = getWidgetCount();
				remove(column - 1);
				remove(column - 2);
				add(addButton);
			}
		});
		inputBox.getTextBox().addKeyPressHandler(new KeyPressHandler() {
			@Override
			public void onKeyPress(KeyPressEvent event) {
				inputBox.showSuggestionList();
			}
		});
		inputBox.addSelectionHandler(new SelectionHandler<Suggestion>() {
			@Override
			public void onSelection(SelectionEvent<Suggestion> event) {
				String s[] = event.getSelectedItem().getReplacementString()
						.split("\"");
				String userName = s[0];
				String email = s[1];
				UserList list = FriendListener.getInstance().friendList;
				String url = null;
				int id = -1;
				for (int i = 0; i < list.size(); i++) {
					if (list.get(i).getEmail().equals(email)) {
						id = list.get(i).getId();
						url = list.get(i).getPortrait();
						break;
					}
				}
				int column = getWidgetCount();
				if (set.contains(id)
						|| RecommendPanel.getInstance().set.contains(id)) {
					MessageDialogBox.show("错误", "你已经在推荐列表或被推荐列表中添加了该用户");
					inputBox.setText("");
					return;
				}
				set.add(id);
				RecommendPanel.getInstance().set.add(id);
				remove(column - 1);
				remove(column - 2);
				add(new RecommendItem(id, userName, url));
				add(addButton);
			}
		});
	}

}
